﻿////////////////////////////////////////////////////////////////////////////////////////////////////
//
//  © 2012 Stanislav S. Yarmonov.
//
//  TODO:LICENSE
//     
////////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Data.Common;

namespace Pandora.Data.Common
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    /// <summary>
    /// Extensions for DbProviderFactory class.
    /// </summary>
    ////////////////////////////////////////////////////////////////////////////////////////////////
    public static class DbProviderFactoryExtensions
    {
        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Database command creation routine.
        /// </summary>
        /// <param name="factory">
        /// Database factory provider to use for command creation.
        /// </param>
        /// <param name="type">
        /// Type of the created database command (table, query, stored procedure).
        /// </param>
        /// <param name="text">
        /// Text of the created database command (table name, query text, stored procedure name).
        /// </param>
        /// <returns>
        /// Returns database command created.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IDbCommand Command(this DbProviderFactory factory, CommandType type, string text)
        {
            IDbCommand command = factory.CreateCommand();
            command.CommandText = text;
            command.CommandType = type;

            command.Connection = factory.CreateConnection();

            return command;
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Stored procedure creation routine.
        /// </summary>
        /// <param name="factory">
        /// Database factory provider to use for command creation.
        /// </param>
        /// <param name="name">
        /// Stored procedure name.
        /// </param>
        /// <returns>
        /// Returns stored procedure database command class.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IDbCommand Procedure(this DbProviderFactory factory, string name)
        {
            return factory.Command(CommandType.StoredProcedure, name);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Table command creation routine.
        /// </summary>
        /// <param name="factory">
        /// Database factory provider to use for command creation.
        /// </param>
        /// <param name="name">
        /// Table name.
        /// </param>
        /// <returns>
        /// Returns database command class for table.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IDbCommand Table(this DbProviderFactory factory, string name)
        {
            return factory.Command(CommandType.TableDirect, name);
        }

        ////////////////////////////////////////////////////////////////////////////////////////////
        /// <summary>
        /// Query command creation routine.
        /// </summary>
        /// <param name="factory">
        /// Database factory provider to use for command creation.
        /// </param>
        /// <param name="text">
        /// Query text.
        /// </param>
        /// <returns>
        /// Returns query database command class.
        /// </returns>
        ////////////////////////////////////////////////////////////////////////////////////////////
        public static IDbCommand Query(this DbProviderFactory factory, string text)
        {
            return factory.Command(CommandType.Text, text);
        }
    }
}
